/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.faces.el;

import java.util.Calendar;
import java.util.Date;


/**
 * Utility used by EL jb:year, jb:month, jb:date
 */
public class CalendarUtil extends Exception {
    static final long serialVersionUID = 31231231231L;

    /**
     * Returns a date of month for a given date.
     * @param date
     * @return date
     */
    public static String getDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        return "" + calendar.get(Calendar.DATE);
    }

    /**
     * Returns month for a given date.
     * @param date
     * @return month
     */
    public static String getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        return "" + calendar.get(Calendar.MONTH);
    }

    public static String getRaceTime(Long date) {
        long raceTime = date.longValue();
        long min = raceTime / 60000;
        long sec = (raceTime - (min * 60000)) / 1000;
        long centsec = (raceTime - (min * 60000) - (sec * 1000)) / 10;
        
        String secStr = ((centsec < 10) ? ("0" + centsec) : ("" + centsec));
        if (min == 0) {
            return sec + "." + secStr;
        } else {
            return min + ":" + sec + "." + secStr;
        }
    }

    /**
     * Returns year for a given date;
     * @param date
     * @return year;
     */
    public static String getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        return "" + calendar.get(Calendar.YEAR);
    }
}
